package com.liutaw.nanjingair.views.activities.base;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.bugtags.library.Bugtags;
import com.liutaw.nanjingair.R;
import com.liutaw.nanjingair.dagger.module.ActivityModule;
import com.liutaw.nanjingair.dagger.component.ApplicationComponent;
import com.liutaw.nanjingair.views.activities.AndroidApplication;
import com.liutaw.nanjingair.views.viewcomponent.LoadingView;
import com.liutaw.nanjingair.views.viewcomponent.TopTipView;
import com.liutaw.navigationbarlib.NaviBarBuilder;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public abstract class BaseActivity extends SwipeBackActivity {
    private LoadingView loadingView;
    private TopTipView topTipView;
    FrameLayout navi_container;
    NaviBarBuilder navigationBarView;
    FrameLayout content_container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getApplicationComponent().inject(this);
        setContentView(R.layout.layout_base_withtitle);
        navi_container = (FrameLayout) findViewById(R.id.navi_container);
        //TODO 是否添加阴影
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            if (needShadow())
//                navi_container.setElevation(getResources().getDimension(R.dimen.top_elevation));
//        }
        content_container = (FrameLayout) findViewById(R.id.content_container);
        View view = LayoutInflater.from(this).inflate(getLayoutResId(), content_container, true);
//        content_container.addView(content_container);
        initCusView(view);
        navigationBarView = new NaviBarBuilder(getApplicationContext());
        if (TextUtils.isEmpty(getCusTitle())) {
            navigationBarView.buildLeftView().buildImg(R.drawable.navi_back_white).buildCenterView().buildTitleText("标题未设定");
        } else
            navigationBarView.buildLeftView().buildImg(R.drawable.navi_back_white).buildCenterView().buildTitleText(getCusTitle());
        initNaviBar(navigationBarView);
        navi_container.addView(navigationBarView.build());
        navigationBarView.getLeftView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //沉浸式状态栏
//        //透明状态栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//        //透明导航栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        loadingView = new LoadingView(this);
        topTipView = new TopTipView(this);
        initOnCreate();
    }

    protected abstract int getLayoutResId();

    protected abstract void initNaviBar(NaviBarBuilder naviBarBuilder);

    protected abstract String getCusTitle();

    protected abstract void initOnCreate();

    protected abstract void initCusView(View view);


    @Override
    public void onBackPressed() {
        super.onBackPressed();
//        useAnimation();
    }

    @Override
    public void finish() {
        super.finish();
        useAnimation();
    }

    public void useAnimation() {
        this.overridePendingTransition(R.anim.slide_right_in, R.anim.slide_right_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        this.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        this.overridePendingTransition(R.anim.slide_left_in, R.anim.slide_left_out);
    }

    public LoadingView getLoadingView() {
        return loadingView;
    }

    public void setLoadingView(LoadingView loadingView) {
        this.loadingView = loadingView;
    }

    public View getContentView() {
        return content_container;
    }

    public View getCusViewById(int resId) {
        return content_container.findViewById(resId);
    }

    public TopTipView getTopTipView() {
        return topTipView;
    }

    public void setTopTipView(TopTipView topTipView) {
        this.topTipView = topTipView;
    }

    public NaviBarBuilder getNavigationBarView() {
        return navigationBarView;
    }

    protected boolean needShadow() {
        return true;
    }
    protected ApplicationComponent getApplicationComponent() {
        return  ((AndroidApplication)getApplication()).getApplicationComponent();
    }

    protected ActivityModule getActivityModule() {
        return new ActivityModule(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //注：回调 1
        Bugtags.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //注：回调 2
        Bugtags.onPause(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        //注：回调 3
        Bugtags.onDispatchTouchEvent(this, event);
        return super.dispatchTouchEvent(event);
    }
}
